package com.service.business.merchant.controller;

import com.service.business.base.dto.LoginUserSession;
import com.service.business.framework.log.RequestLog;
import com.service.business.merchant.controller.rep.CommodityDetailRep;
import com.service.business.merchant.controller.rep.CommodityPageRep;
import com.service.business.merchant.controller.req.CommoditySaveReq;
import com.service.business.merchant.controller.req.CommodityUpdateReq;
import com.service.business.merchant.service.MerchantCommodityService;
import com.service.business.merchant.service.MerchantService;
import com.service.business.support.SessionUtil;
import com.service.core.exception.CommonException;
import com.service.core.rest.BaseController;
import com.service.core.rest.request.PageRequest;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.OneResponse;
import com.service.core.rest.response.PageResponse;
import com.service.dao.enums.MerchantCommodityEnum;
import com.service.dao.model.Merchant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 商家商品信息
 *
 * @author xxxx
 * @email xxxx@qq.com
 * @date 2020-03-28 12:54:56
 */
@RestController
@RequestMapping("merchant-commodity")
public class MerchantCommodityController extends BaseController {
    @Autowired
    private MerchantCommodityService merchantCommodityService;

    @PostMapping("{merchantNo}/page")
    @RequestLog("某个商家商品分页查询")
    public PageResponse<CommodityPageRep> page(@PathVariable("merchantNo") String merchantNo, @RequestBody PageRequest pageRequest) {
        return successQueryPageResponse(merchantCommodityService.page(merchantNo, pageRequest));
    }

    @PostMapping("manager-page")
    @RequestLog("商家商品分页查询")
    public PageResponse<CommodityPageRep> managerPage(@RequestBody PageRequest pageRequest) {
        return successQueryPageResponse(merchantCommodityService.page(null, pageRequest));
    }

    @PostMapping("save")
    @RequestLog("添加商家商品")
    public BaseResponse save(@RequestBody @Validated CommoditySaveReq saveReq) {
        merchantCommodityService.addCommodity(saveReq);
        return successBaseResponse();
    }

    @GetMapping("detail/{commodityNo}")
    @RequestLog("商品详情")
    public OneResponse<CommodityDetailRep> detail(@PathVariable("commodityNo") String commodityNo) {
        return successQueryOneResponse(merchantCommodityService.detail(commodityNo));
    }

    @PostMapping("update/{commodityNo}")
    @RequestLog("编辑商家商品")
    public BaseResponse update(@PathVariable("commodityNo") String commodityNo, @RequestBody @Validated CommodityUpdateReq updateReq) {
        merchantCommodityService.updateCommodity(commodityNo, updateReq);
        return successBaseResponse();
    }

    @PostMapping("put-away/{commodityNo}")
    @RequestLog("上架商家商品")
    public BaseResponse putAway(@PathVariable("commodityNo") String commodityNo) {
        merchantCommodityService.updateStatus(commodityNo, MerchantCommodityEnum.Status.PUT_AWAY);
        return successBaseResponse();
    }

    @PostMapping("take-off/{commodityNo}")
    @RequestLog("下架商家商品")
    public BaseResponse takeOff(@PathVariable("commodityNo") String commodityNo) {
        merchantCommodityService.updateStatus(commodityNo, MerchantCommodityEnum.Status.TAKE_OFF);
        return successBaseResponse();
    }


}
